﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace Kiaser.DataLibrary
{
    public class Repository<T> where T :class
    {
        public DbContext DbContext { get; set; }

        public Repository()
        { }

        /// <summary>
        /// 构造函数，
        /// </summary>
        /// <param name="dbContext">数据库上下文</param>
        public Repository(DbContext dbContext)
        {
            DbContext = dbContext;
        }

        //查找实体
        #region Find
        /// <summary>
        /// 查找实体
        /// </summary>
        /// <param name="ID">实体主键值</param>
        /// <returns></returns>
        public T Find(int ID)
        {
           return DbContext.Set<T>().Find(ID);
        }

        /// <summary>
        /// 查找实体
        /// </summary>
        /// <param name="where">查询的lambda表达式</param>
        /// <returns></returns>
        public T Find(Expression<Func<T, bool>> where)
        {
            return DbContext.Set<T>().SingleOrDefault(where);
        }
        #endregion

        //查找实体列表
        #region FindList

        /// <summary>
        /// 查找实体列表
        /// </summary>
        /// <returns></returns>
        public IQueryable<T> FindList()
        {
            return DbContext.Set<T>();
        }

        /// <summary>
        /// 查找实体列表
        /// </summary>
        /// <param name="where">查询的lambda表达式</param>
        /// <returns></returns>
        public IQueryable<T> FindList(Expression<Func<T, bool>> where)
        {
            return DbContext.Set<T>().Where(where);
        }

        /// <summary>
        /// 查找实体键类型
        /// </summary>
        /// <param name="where">查询lambda表达式</param>
        /// <param name="num">获取记录的数量</param>
        /// <returns></returns>
        public IQueryable<T> FindList(Expression<Func<T, bool>> where, int num)
        {
            return DbContext.Set<T>().Where(where).Take(num);
        }

        /// <summary>
        /// 查找实体列表
        /// </summary>
        /// <typeparam name="TKey">排序建类型</typeparam>
        /// <param name="order">排序键</param>
        /// <param name="asc">是否正序</param>
        /// <returns></returns>
        public IQueryable<T> FindList<TKey>(Expression<Func<T, TKey>> order, bool asc)
        {
            return asc ? DbContext.Set<T>().OrderBy(order) : DbContext.Set<T>().OrderByDescending(order);
        }

        /// <summary>
        /// 查找实体列表
        /// </summary>
        /// <typeparam name="TKey">排序键类型</typeparam>
        /// <param name="order">排序建</param>
        /// <param name="asc">是否正序</param>
        /// <param name="num">获取记录的数量</param>
        /// <returns></returns>
        public IQueryable<T> FindList<TKey>(Expression<Func<T, TKey>> order, bool asc, int num)
        {
            return asc ? DbContext.Set<T>().OrderBy(order).Take(num) : DbContext.Set<T>().OrderByDescending(order).Take(num);
        }

        /// <summary>
        /// 查找实体列表
        /// </summary>
        /// <typeparam name="Tkey">排序键类型</typeparam>
        /// <param name="where">查询lambda表达式</param>
        /// <param name="order">排序键</param>
        /// <param name="asc">是否正序</param>
        /// <returns></returns>
        public IQueryable<T> FindList<Tkey>(Expression<Func<T, bool>> where, Expression<Func<T, Tkey>> order, bool asc)
        {
            return asc ? DbContext.Set<T>().Where(where).OrderBy(order) : DbContext.Set<T>().Where(where).OrderByDescending(order);
        }

        /// <summary>
        /// 查找实体列表
        /// </summary>
        /// <typeparam name="TKey">排序建类型</typeparam>
        /// <param name="where">查询的lambda表达式</param>
        /// <param name="order">排序建</param>
        /// <param name="asc">是否正序</param>
        /// <param name="num">获取记录的数量</param>
        /// <returns></returns>
        public IQueryable<T> FindList<TKey>(Expression<Func<T,bool>> where,Expression<Func<T,TKey>> order,bool asc,int num)
        {
            return asc ? DbContext.Set<T>().Where(where).OrderBy(order).Take(num) : DbContext.Set<T>().Where(where).OrderByDescending(order).Take(num);
        }
        #endregion


        //查找分页列表
        #region FindPageList

        /// <summary>
        /// 查找分页列表
        /// </summary>
        /// <param name="pageSize">每页记录数，必须大于1</param>
        /// <param name="pageIndex">页码，首页重1开始，必须大于1</param>
        /// <param name="totalNumber">总记录数</param>
        /// <returns></returns>
        public IQueryable<T> FindPageList(int pageSize, int pageIndex, out int totalNumber)
        {
            if (pageIndex < 1) pageIndex = 1;
            if (pageSize < 1) pageSize = 10;
            IQueryable<T> _list = DbContext.Set<T>();
            totalNumber = _list.Count();
            return _list.Skip((pageIndex - 1) * pageSize).Take(pageSize);

        }
        /// <summary>
        /// 查找分页列表
        /// </summary>
        /// <typeparam name="TKey">排序键类型</typeparam>
        /// <param name="pageSize">每页记录数，必须大于1</param>
        /// <param name="pageIndex">页码，首页重1开始，必须大于1</param>
        /// <param name="totalNumber">总记录数</param>
        /// <param name="order">排序键</param>
        /// <param name="asc">是否正序</param>
        /// <returns></returns>

        public IQueryable<T> FindPageList<TKey>(int pageSize, int pageIndex, out int totalNumber, Expression<Func<T, TKey>> order, bool asc)
        {
            if (pageIndex < 1) pageIndex = 1;
            if (pageSize < 1) pageSize = 10;
            IQueryable<T> _list = DbContext.Set<T>();
            _list = asc ? _list.OrderBy(order) : _list.OrderByDescending(order);
            totalNumber = _list.Count();
            return _list.Skip((pageIndex - 1) * pageSize).Take(pageSize);
        }

        /// <summary>
        /// 查找分页列表
        /// </summary>
        /// <param name="pageSize">每页记录数，必须大于1</param>
        /// <param name="pageIndex">页码，首页重1开始，必须大于1</param>
        /// <param name="totalNumber">总记录数</param>
        /// <param name="where">查询的lambda表达式</param>
        /// <returns></returns>
        public IQueryable<T> FindPageList(int pageSize, int pageIndex, out int totalNumber, Expression<Func<T, bool>> where)
        {
            if (pageSize < 1) pageSize = 10;
            if (pageIndex < 1) pageIndex = 1;
            IQueryable<T> _list = DbContext.Set<T>().Where(where);
            totalNumber = _list.Count();
            return _list.Skip((pageIndex - 1) * pageSize).Take(pageSize);

        }

        /// <summary>
        /// 查找分页列表
        /// </summary>
        /// <typeparam name="TKey">排序键类型</typeparam>
        /// <param name="pageSize">每页记录数，必须大于1</param>
        /// <param name="pageIndex">页码，首页重1开始，必须大于1</param>
        /// <param name="totalNumber">总记录数</param>
        /// <param name="where">查询lambda表达式</param>
        /// <param name="order">排序键</param>
        /// <param name="asc">是否正序</param>
        /// <returns></returns>
        public IQueryable<T> FindPageList<TKey>(int pageSize, int pageIndex, out int totalNumber, Expression<Func<T, bool>> where, Expression<Func<T, TKey>> order, bool asc)
        {
            if (pageSize < 1) pageSize = 10;
            if (pageIndex < 1) pageIndex = 1;
            IQueryable<T> _list = DbContext.Set<T>().Where(where);
            _list = asc ? _list.OrderBy(order) : _list.OrderByDescending(order);
            totalNumber = _list.Count();
            return _list.Skip((pageIndex - 1) * pageSize).Take(pageSize);
        }
        #endregion

        //添加实体
        #region Add

        /// <summary>
        /// 添加实体
        /// </summary>
        /// <param name="entity">实体类</param>
        /// <returns></returns>
        public int Add(T entity)
        {
            return Add(entity, true);
        }
        /// <summary>
        /// 添加实体
        /// </summary>
        /// <param name="entity">实体类</param>
        /// <param name="isSave">是否保存</param>
        /// <returns>返回受影响行数,false就返回0</returns>
        public int Add(T entity, bool isSave)
        {
            DbContext.Set<T>().Add(entity);
            return isSave ? DbContext.SaveChanges() : 0;
        }
        #endregion

        //修改实体
        #region Update

        /// <summary>
        /// 修改实体
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public int Update(T entity)
        {
            return Update(entity, true);
        }
        /// <summary>
        /// 修改实体
        /// </summary>
        /// <param name="entity">实体类</param>
        /// <param name="isSave">是否保存</param>
        /// <returns>返回受影响行数,false就返回0</returns>
        public int Update(T entity, bool isSave)
        {
            DbContext.Set<T>().Attach(entity);
            DbContext.Entry<T>(entity).State = EntityState.Modified;
            return isSave ? DbContext.SaveChanges() : 0;
        }
        #endregion

        //删除实体
        #region Delete
        /// <summary>
        /// 删除实体
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public int Delete(T entity)
        {
            return Delete(entity, true);
        }
        /// <summary>
        /// 删除实体
        /// </summary>
        /// <param name="entity">实体类</param>
        /// <param name="isSave">是否保存</param>
        /// <returns>返回受影响行数，false就返回0</returns>
        public int Delete(T entity, bool isSave)
        {
            DbContext.Set<T>().Remove(entity);
            return isSave ? DbContext.SaveChanges() : 0;
        }
        #endregion

        //记录数
        #region Count
        /// <summary>
        /// 记录数
        /// </summary>
        /// <returns></returns>
        public int Count()
        {
            return DbContext.Set<T>().Count();
        }
        /// <summary>
        /// 记录数
        /// </summary>
        /// <param name="where">查询lambda表达式</param>
        /// <returns></returns>
        public int Count(Expression<Func<T, bool>> predicate)
        {
            return DbContext.Set<T>().Count(predicate);
        }
        #endregion

        /// <summary>
        /// 记录是否存在
        /// </summary>
        /// <param name="predicate">查询lambda表达式</param>
        /// <returns></returns>
        public bool IsContains(Expression<Func<T, bool>> predicate)
        {
            return Count(predicate) > 0;
        }
        /// <summary>
        /// 保存数据
        /// </summary>
        /// <returns></returns>
        public int Save()
        {
            return DbContext.SaveChanges();
        }
    }
}
